			* Code module vidset2.asm
			* Setting CoCo 1/2 hires graphics modes 
                	* Normal start of text screen 			$0400
                	* Preferred start of 1st graphics screen page	$0E00
                	* Preferred start of 2nd graphics screen page	$2500
START	EQU	$3F00	* Start address
WARMST	EQU	$80C0	* Warm start (back to basic)
WAIT	EQU	$ADFB	* Color Basic keypress wait routine
PIA1BD  EQU	$FF22	* PIA1 data port B: VDG Control output

	ORG	START
MAIN			*
VDDEMO	NOP		* Video display setting demo entry point
	LDD	$0E00	* set video display offset parameter (Preferred start of 1st graphics screen page on DECB systems)
	JSR	VOFSET	* call video display offset method
	LDA	#$F8	* value for mode 1111 color set 1 - PMODE 4 / SCREEN 1,1 equivalent
	JSR	VIDSET	* set graphics mode 
	JSR	CLRVID	* clear screen
	JSR 	PATTRN	* draw pattern
	JSR	WAIT	* wait for key press
	LDA	#$F0	* value for mode 1111 color set 0 - PMODE 4 / SCREEN 1,0 equivalent
	JSR	VIDSET	* set graphics mode 
	JSR	WAIT	* wait for key press
	LDA	#$E8	* value for mode 1110 color set 1 - PMODE 3 / SCREEN 1,1 equivalent
	JSR	VIDSET	* set graphics mode 
	JSR	WAIT	* wait for key press
	LDA	#$E0	* value for mode 1110 color set 0 - PMODE 3 / SCREEN 1,0 equivalent
	JSR	VIDSET	* set graphics mode
	JSR	WAIT	* wait for key press
	LDA	#$00	* value for mode 0000 0 (normal text screen)
	JSR	VIDSET	* set text mode
	JSR	WAIT	* wait for key press
	JMP	WARMST	* warm start (back to basic)
			*
			* Routine to draw test screen pattern
PATTRN	LDA	#$C6	* load bit pattern to draw for demo (11 00 01 10) $C6 -58
	LDX	#$0E00	* point to start address on screen
VS2L02	STA	,X+	* write bits and increment pointer
	CMPX	#$1200	* check for end address on screen
	BCS	VS2L02	* loop if not end address 
	LDA	#$AA	* load bit pattern to draw for demo (10 10 10 10) $AA -86
	LDX	#$1200	* point to start address on screen
VS2L03	STA	,X+	* write bits and increment pointer
	CMPX	#$1400	* check for end address on screen
	BCS	VS2L03	* loop if not end address 
	LDA	#$55	* load bit pattern to draw for demo (01 01 01 01) $55 85
	LDX	#$1400	* point to start address on screen
VS2L04	STA	,X+	* write bits and increment pointer
	CMPX	#$1600	* check for end address on screen
	BCS	VS2L04	* loop if not end address 
	LDA	#$FF	* load bit pattern to draw for demo (11 11 11 11) $FF -1
	LDX	#$1600	* point to start address on screen
VS2L05	STA	,X+	* write bits and increment pointer
	CMPX	#$1800	* check for end address on screen
	BCS	VS2L05	* loop if not end address 
	RTS
			*
			* Smart routine to set video mode 	* example data:
VIDSET	STA	PIA1BD	* storing at PIA 1 data port B-side	* A: %1111 1000 
	TFR	A,B	* transfer parameter to B as counter 	* B: %1111 1000 
	LSRB		* shift least significant nibble (bit0)	* B: %0111 1100
	LSRB		*    "     "        "         "  (bit1)	* B: %0011 1110
	LSRB		*    "     "        "         "  (bit2)	* B: %0001 1111
	LSRB		*    "     "        "         "  (bit3)	* B: %0000 1111
	LDX	VTABEL	* point to video settings table			 
VSLOOP	LDA	,X+	* get table value				 
	DECB		* decrement counter until zero			
	BNE	VSLOOP	* loop until zero			* (16 times)   
	STA	VSVAL	* remember video setting value		* A: %0011 0110
	ANDA	$F0	* blank out least significant nibble	* A: %0011 0000
	CLRB		* clear least significant byte		* D: %0011 0000 0000 0000
	LSRA		* adjust page size: move MSnibble 1 bit	* D: %0001 1000 0000 0000
	ADDD	VOFFST	* add offset & pagesize to get page end	* D: %0010 0110 0000 0000
	STD	VPGEND	* store page end address 		* D: $2600
	LDA	VSVAL	* restore vs value to handle SAM V set	* A: %0011 0110
	JSR	VMVSET	* set SAM V register 
	RTS	
			* 
			* Smart routine to set SAM V register	* example parameter: $1110 0000
VMVSET	NOP		* entry point                           * 
	LDB	#3	* set counter (3 bits in SAM V register)* 
	LDX	$FFC0	* point to first SAM F bit clear byte   * 
	LSLA		* converting parameter for SAM V setting* 1 <- $110 0 0000
VMLOOP	RORA		* test A bit                            * 1 <- $100 0 0001 <-
	BCC	VMSKP1	* skip if bit not set                   * 
	LEAX	1,X	* skip even offset byte                 * 
	STA	,X+	* set corresponding V bit               * 
	BRA	VMSKP2	* skip on                               * 
VMSKP1	STA	,X++	* set both offset bytes                 * 
VMSKP2	DECB		* decrement counter                     * 
	BNE	VMLOOP	* loop until counter is zero
	RTS	
			*
			* Clear screen routine
CLRVID	LDD	#$0000	* load empty word 
	LDX	#VOFFST	* point to screen start address		
VS2L01	STD	,X++	* clear bits and increment pointer
	CMPX	#VPGEND	* check for end of screen 
	BCS	VS2L01	* loop if not end
	RTS
			*
			* Smart routine to set video offset SAM F 
			* sets video offset as specified in D register
			* FFC6/FFC7 clear/set F0 - $0200
			* FFC8/FFC8 clear/set F1 - $0400
			* FFCA/FFCB clear/set F2 - $0800
			* FFCC/FFCD clear/set F3 - $1000
			* FFCE/FFCF clear/set F4 - $2000
			* FFD0/FFD1 clear/set F5 - $4000
			* FFD2/FFD3 clear/set F6 - $8000
			* Preferred start of 1st graphics screen page on DECB systems. $0E00 
			* Preferred start of 2nd graphics screen page on DECB systems: $2500 
VOFSET	STD	VOFFST	* remember video offset value 
	LDB	#7	* set counter (7 bits in SAM F register)
	LDX	$FFC6	* point to first SAM F bit clear byte
	LSRA		* convert MSB of D (A) to value to be set in SAM F register
VOLOOP	RORA		* test A bit
	BCC	VOSKP1	* skip if bit not set
	LEAX	1,X	* skip even offset byte (increment pointer)  
	STA	,X+	* write to odd offset byte -> set corresponding F bit
	BRA	VOSKP2	* skip on
VOSKP1	STA	,X++	* set both offset bytes (clears F bit, because even byte is written to last)
VOSKP2	DECB		* decrement counter
	BNE	VOLOOP	* loop until counter is zero
	RTS
			* 		
			* Data area 
VOFFST	FDB	$0000	* video offset variable (current start of video RAM)	* e.g.: %0000 1110 0000 0000
VSVAL	FCB	$00	* variable to hold temp video settings value
VPGEND	FDB	$0000	* variable to hold video page end 
VTABEL	FCB     $10	* Internal alphanumeric 
        FCB     $10     * External alphanumeric                 
        FCB     $10     * Semigraphic-4	                       
        FCB     $10     * Semigraphic-6	              
        FCB     $10     * Internal alphanumeric         
        FCB     $10     * External alphanumeric        
        FCB     $10     * Semigraphic-4	             
        FCB     $10     * Semigraphic-6	              
        FCB     $21     * Full graphic 1-C      %1000 X000                
        FCB     $21     * Full graphic 1-R      %1000 X000                
        FCB     $42     * Full graphic 2-C      %1010 X000      
        FCB     $33     * Full graphic 2-R      %1010 X000      
        FCB     $34     * Full graphic 3-C      %1100 X000                
        FCB     $35     * Full graphic 3-R      %1100 X000               
        FCB     $36     * Full graphic 6-C      %1110 X000      
        FCB     $36     * Full graphic 6-R      %1111 X000      
	END	MAIN
	
























